﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Frame.Lib.Web;

namespace Jzy.Core.Common.Web
{
    public class CustSort
    {      
        private BindViewHandler _bindView;
        private PagerSortSetting _setting;
        private IButtonControl[] _buttons;//表头上排序按钮

        public CustSort(PagerSortSetting setting, BindViewHandler bindView)
        {         
            _setting = setting;
            _bindView = bindView;
        }

        public void Sort(GridView gv)
        {
            gv.AllowSorting = true;
            gv.Sorting += new GridViewSortEventHandler(gv_Sorting);
            gv.DataBound += new EventHandler(gv_DataBound);
        }

        
        void gv_Sorting(object sender, GridViewSortEventArgs e)
        {
            SwapSortDirection(_setting);
            _setting.SortExpression = e.SortExpression;
            _setting.PageIndex = 1;
            _bindView();

        }

        void lv_Sorting(object sender, ListViewSortEventArgs e)
        {
            SwapSortDirection(_setting);
            _setting.SortExpression = e.SortExpression;
            _setting.PageIndex = 1;
            _bindView();
        }

        void gv_DataBound(object sender, EventArgs e)
        {
            GridView gvList = (GridView)sender;

            int columnIndex = -1;
            foreach (DataControlField field in gvList.Columns)
            {
                if (field.SortExpression == _setting.SortExpression)
                {
                    columnIndex = gvList.Columns.IndexOf(field);
                    break;
                }
            }
            if (gvList.HeaderRow != null)
            {
                if (columnIndex != -1)
                {
                    Image imgSort = new Image();
                    imgSort.ImageAlign = ImageAlign.AbsMiddle;
                    if (_setting.SortDirection == Frame.Lib.Web.SortDirection.asc)
                    {
                        imgSort.ImageUrl = "~/common/images/common/asc.gif";
                    }
                    else
                    {
                        imgSort.ImageUrl = "~/common/images/common/desc.gif";
                    }
                    Literal litSpace = new Literal();
                    litSpace.Text = "&nbsp;";
                    gvList.HeaderRow.Cells[columnIndex].Controls.Add(litSpace);
                    gvList.HeaderRow.Cells[columnIndex].Controls.Add(imgSort);
                }
            }
        }

        public static void SwapSortDirection(PagerSortSetting setting)
        {
            if (setting.SortDirection == Frame.Lib.Web.SortDirection.asc)
            {
                setting.SortDirection = Frame.Lib.Web.SortDirection.desc;
            }
            else
            {
                setting.SortDirection = Frame.Lib.Web.SortDirection.asc;
            }            
        }

       
        /// <summary>
        /// 用于ListView的排序事件，传表头的的Button
        /// </summary>
        /// <param name="buttons"></param>
        public void Sort(params IButtonControl[] buttons)
        {
            this._buttons = buttons;
            foreach (IButtonControl button in buttons)
            {
                button.Click += new EventHandler(button_Click);
            }
        }

        /// <summary>
        /// 设置初始化时排序图标
        /// </summary>
        /// <param name="button"></param>
        public void SetInitSort()
        {
            foreach (IButtonControl btn in _buttons)
            {
                if (btn.CommandArgument == _setting.SortExpression)
                {
                    SetImage(btn);
                }
            }
        }


        private void SetImage(object sender)
        {
            IButtonControl button = (IButtonControl)sender;
            Image imgSort = new Image();
            imgSort.ImageAlign = ImageAlign.AbsMiddle;
            if (_setting.SortDirection == Frame.Lib.Web.SortDirection.asc)
            {
                imgSort.ImageUrl = "~/common/images/common/asc.gif";
            }
            else
            {
                imgSort.ImageUrl = "~/common/images/common/desc.gif";
            }
            Literal litSpace = new Literal();
            litSpace.Text = "&nbsp;";

            Control ctl = (Control)sender;
            int index = ctl.Parent.Controls.IndexOf(ctl) + 1;
            ctl.Parent.Controls.AddAt(index, litSpace);
            ctl.Parent.Controls.AddAt(index + 1, imgSort);
        }

        private void button_Click(object sender, EventArgs e)
        {
            SwapSortDirection(_setting);
            //SetImage(sender);         
 
            IButtonControl button = (IButtonControl)sender;          
            _setting.SortExpression = button.CommandArgument;
            _setting.PageIndex = 1;            
            _bindView();
            
        }
    }
}
